package com.echo.register.commons.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @program: com.echo.voter
 * @description:
 * @author: Xiaoti
 * @create: 2018-11-02 10:25
 **/
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    public WebConfig() {
        super();
    }
    //因为新加了拦截器，这里需要重新设置资源地址
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations(
                "classpath:/templates/");

        super.addResourceHandlers(registry);
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://127.0.0.1")
                .allowedMethods("GET", "POST")
                .allowCredentials(false).maxAge(3600);
    }

//    @Bean
//    public FilterRegistrationBean httpFilter(){
//        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
//        registrationBean.setFilter(new VoteFilter());
//        registrationBean.addUrlPatterns("=info/vote.do");
//        return registrationBean;
//    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截规则：除了login，其他都拦截判断,excludePathPatterns是排除拦截的路径，一个是登录验证地址，一个是登录页
        registry.addInterceptor(new VoteInterceptor()).addPathPatterns("/info/vote.do", "/user/login.do");
        super.addInterceptors(registry);
    }

}
